package com.example.demo.service;

import com.example.demo.mapper.domain.Images;
import com.example.demo.mapper.domain.ImagesExample;
import com.example.demo.mapper.ImagesMapper;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.Wordlist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class Imageservice {
    @Autowired
    public ImagesMapper imagesMapper;

    public List<Images> findid(String id){
        ImagesExample imagesExample=new ImagesExample();
        ImagesExample.Criteria criteria=imagesExample.createCriteria();
        criteria.andArticleidEqualTo(id);
        List<Images> imagesList= imagesMapper.selectByExample(imagesExample);
        return imagesList;
    }
    public List<Images> all(){
        ImagesExample imagesExample=new ImagesExample();
        ImagesExample.Criteria criteria=imagesExample.createCriteria();
        criteria.andImagesIsNotNull();
        List<Images> imagesList= imagesMapper.selectByExample(imagesExample);
        Comparator<Images> comparator = Comparator.comparing(Images::getTime).reversed();
        Collections.sort(imagesList, comparator);
        return imagesList;
    }
    public boolean delet(String id){
        ImagesExample imagesExample=new ImagesExample();
        ImagesExample.Criteria criteria=imagesExample.createCriteria();
        criteria.andArticleidEqualTo(id);
        int insert=imagesMapper.deleteByExample(imagesExample);
        if (insert==0)
        {
            return false;
        }else{
            return true;
        }
    }
    public boolean add(Images images)
    {
        int insert=imagesMapper.insert(images);
        if (insert==0)
        {
            return false;
        }else{
            return true;
        }
    }
}
